// 页面路径：store/index.js 
import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex); //vue的插件机制
import M_address  from './address.js'

//Vuex.Store 构造器选项
const store = new Vuex.Store({
	state: { //存放状态
		"username": "foo",
		"age": 18,
		goods_count: uni.getStorageSync('cart').length,
		cart: uni.getStorageSync('cart') || [],
		
		// address:uni.getStorageSync('address')
	},
	mutations: {
		// updateAddress(state,address){
		// 	state.address = address
		// 	uni.setStorageSync('address',state.address) 
		// },
		
		
		deleteOne(state,index){

			state.cart.splice(index,1)
			this.commit('saveToStorage')
			console.log(state.cart);
		},
		addToCart(state, goods) {
			let res= goods
			state.cart.push(res)
			 this.commit('saveToStorage')
		},
		changeNumberValue(state,value){
			state.cart[value.activeIndex].goods_count = value.value
			uni.setStorageSync('cart',state.cart)
		},
		saveToStorage(state) {
			uni.setStorageSync('cart', state.cart)
		}
	},
	getters: {
		total(state) {
			let len = state.cart.length
			return len
		}
	},
	modules:{
		M_address
	}
})
export default store